package aplicacion;

import figuras.PuntoVector;
import gui.JPanels.Settings;
import gui.jmenubar.JMenuBarMain;
import gui.jtab.JTab;
import gui.jtab.estambre.JTabObjectEstambre;
import gui.jtab.flower.JTabCirculos;
import gui.jtab.flower.JTabFlor;
import gui.jtab.leaf.JTabObjectLeaf;
import gui.jtab.leaf.JTabObjectSepal;
import gui.jtab.pistilo.JTabObjectPistilo;
import gui.jtab.receptaculo.JTabObjectReceptaculo;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JTabbedPane;

import textures.BitmapLoader;
import textures.TextureReader;
import constantes.Consts;


public class Vista extends JFrame{
	
	public static boolean cargar;
	public static boolean editarInversiones;

	private static final long serialVersionUID = 1L;
	private JTabCirculos circulos;
	private JTabFlor flor;
	private Controlador control;
	private JTabbedPane pestana;
	
	private JMenuBarMain jMenuBar;
	
	private static Vista instanceVista;
	
	public Vista(){
		setIcon();
		setTitle("Flower Designer 3D");
		this.control = new Controlador();
		cargar = false;
		editarInversiones = true;
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				control.borraTmp();
				System.exit(0);
			}
		});
	}
	
	private void setIcon() {
		try {
			BufferedImage bufferedImage;
			String imagePath = Consts.IMAGES_FOLDER+Consts.IMAGE_ICON;
			if (imagePath.endsWith(".bmp"))
			    bufferedImage = BitmapLoader.loadBitmap(imagePath);
			else
			    bufferedImage = TextureReader.readImage(imagePath);
			setIconImage(bufferedImage);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	public void run(){
		
		pestana = new JTabbedPane();
		pestana.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jTabbedPane1StateChanged(evt);
            }
        });
		pestana.add(circulos, "Flor");
		pestana.add(new JTabObjectPistilo(), "Pistilos");
		pestana.add(new JTabObjectEstambre(), "Estambres");
		pestana.add(new JTabObjectLeaf(), "Petalos");
		pestana.add(new JTabObjectSepal(), "Sepalos");
		pestana.add(new JTabObjectReceptaculo(),"Receptaculos");

		this.getContentPane().add(pestana,BorderLayout.CENTER );
		this.setJMenuBar(getJMenuBarApp());
		jMenuBar.setJTab((JTab)pestana.getSelectedComponent());
		
		this.setSize(1124,600);
		setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	

    private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {
    	try {
			if(jMenuBar!=null)
				jMenuBar.setJTab((JTab)pestana.getSelectedComponent());
		} catch (RuntimeException e) {
			jMenuBar.setJTab(null);
		}
     }
	
	private JMenuBar getJMenuBarApp() {
		if (jMenuBar == null) {
			jMenuBar = new JMenuBarMain();
		}
		return jMenuBar;
	}

	public Controlador getControl() {
		return control;
	}
	
	public void setCiculos(JTabCirculos circulos) {
		this.circulos = circulos;
	}
	
	public void setControl(Controlador control) {
		this.control = control;
	}
	
	public void setFlor(JTabFlor flor) {
		this.flor = flor;
	}

	public void cargaPanelFlor(String file) {
		
		control.insertaFileReceptaculo(file);
		
		pestana.setComponentAt(0, flor);	
		jMenuBar.setJTab((JTab)pestana.getSelectedComponent());
		flor.removeAll();
		flor.iniciaComponents();
		pestana.repaint();
	}
	
	public void volver() {
		pestana.setComponentAt(0, circulos);
		jMenuBar.setJTab((JTab)pestana.getSelectedComponent());
		//circulos.repaint();
		pestana.repaint();
	}
	
	public void openLib(PuntoVector mouse) {

		int[] data = control.openLibrary(mouse);
		
		if(data!=null){
			flor.removeAll();
			flor.iniciaComponentsLib(data[0], data[1]);
			pestana.repaint();
		}		
	}
	
	public void openLib(int c) {

		flor.removeAll();
		flor.iniciaComponentsLib(c, Integer.MAX_VALUE);
		pestana.repaint();		
	}
	
	public void openEmptyPanel(){
		flor.removeAll();
		flor.iniciaComponents();
		pestana.repaint();

	}
	
	public static synchronized Vista getInstance()  {
		if(instanceVista==null){
			instanceVista=new Vista();
		}
		return instanceVista;
	}
	
	public void nuevoPestanaFlor(){
		
		control.borraFilesDataFlower();
		flor.removeAll();
		flor.iniciaComponents();
		pestana.repaint();
	}
	
	public void actualizaSettings(){
		Settings set = (Settings) circulos.getSettings();
		set.actualiza();
	}

	public static boolean isCargar() {
		return cargar;
	}

	public static void setCargar(boolean cargar) {
		Vista.cargar = cargar;
	}

	public static boolean isEditarInversiones() {
		return editarInversiones;
	}

	public static void setEditarInversiones(boolean editarInversiones) {
		Vista.editarInversiones = editarInversiones;
	}
}
